﻿Imports Exp.DATA_Service
Imports Exp.Model
Imports Exp.DATA_Service.Exp.DATA_Service
Public Class ucBookingView
    Inherits System.Web.UI.UserControl
    Private _bookingid As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindCountry()
            BindBrand()
            BindResort()
            BindUnitType()
            BindYear()
            BindMonth()

        End If
    End Sub
#Region "Function"
   
    Sub BindResort(Optional ByVal ibrandID As Integer = 0)
        Dim REM_Resort As New List(Of REM_Resort)
        Dim Resort_Service As New Resort_service
        REM_Resort = Resort_Service.GetAll()
        REM_Resort = REM_Resort.Where(Function(p) p.Brand_ID = ibrandID).ToList()
        REM_Resort.Distinct()
        ddlResortSearch.DataSource = REM_Resort
        ddlResortSearch.DataTextField = "ResortName"
        ddlResortSearch.DataValueField = "Resort_ID"
        ddlResortSearch.DataBind()
        ddlResortSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindBrand()
        Dim REM_Brand As New List(Of REM_Brand)
        Dim Brand_Service As New Brand_Service
        REM_Brand = Brand_Service.GetAll()
        REM_Brand.Distinct()
        ddlBrandSearch.DataSource = REM_Brand
        ddlBrandSearch.DataTextField = "BrandName"
        ddlBrandSearch.DataValueField = "Brand_ID"
        ddlBrandSearch.DataBind()
        ddlBrandSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindUnitType()
        Dim REM_UnitType As New List(Of REM_UnitType)
        Dim UnitType_Service As New UnitType_service
        REM_UnitType = UnitType_Service.GetAll()
        'REM_UnitType = REM_UnitType.Where(Function(p) p.UnitType_ID = ibrandID).ToList()
        REM_UnitType.Distinct()
        ddlUnitTypeSearch.DataSource = REM_UnitType
        ddlUnitTypeSearch.DataTextField = "UnitTypeName"
        ddlUnitTypeSearch.DataValueField = "UnitType_ID"
        ddlUnitTypeSearch.DataBind()
        ddlUnitTypeSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindCountry()
        Dim REM_Country As New List(Of REM_Country)
        Dim Country_service As New Country_Service
        REM_Country = Country_service.GetAll()
        REM_Country.Distinct()
        ddlCountrySearch.DataSource = REM_Country
        ddlCountrySearch.DataTextField = "CountryName"
        ddlCountrySearch.DataValueField = "Country_ID"
        ddlCountrySearch.DataBind()
        ddlCountrySearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindMonth()
        Dim strMonth As String = Month(Now)
        ddlMonthSearch.SelectedValue = ddlMonthSearch.Items.FindByValue(strMonth).Value
    End Sub
    Sub BindYear()
        Dim iYear As Integer
        iYear = CType(Format(Now, "yyyy"), Integer)
        If iYear > 2500 Then
            iYear = iYear - 543
        End If
        ddlYearSearch.Items.Add(iYear)
        ddlYearSearch.Items.Add(iYear + 1)
    End Sub
    Sub bindData()
        Dim lstobj As New List(Of EXP_Select_RET_Booking_And_REM_Unit_Result)
        Dim Unit_Service As New Unit_Service
        lstobj = Unit_Service.GetEXP_Select_RET_Booking_And_REM_Unit(ddlMonthSearch.SelectedValue, ddlYearSearch.SelectedValue).ToList()
        If ddlBrandSearch.SelectedValue <> "" Then
            lstobj = lstobj.Where(Function(p) p.Brand_ID = ddlBrandSearch.SelectedValue).ToList()
        End If
        If ddlCountrySearch.SelectedValue <> "" Then
            lstobj = lstobj.Where(Function(p) p.country_ID = ddlCountrySearch.SelectedValue).ToList()
        End If
        If ddlResortSearch.SelectedValue <> "" Then
            lstobj = lstobj.Where(Function(p) p.Resort_ID = ddlResortSearch.SelectedValue).ToList()
        End If
        If ddlUnitTypeSearch.SelectedValue <> "" Then
            lstobj = lstobj.Where(Function(p) p.UnitType_ID = ddlUnitTypeSearch.SelectedValue).ToList()
        End If

        gv_data.DataSource = lstobj
        gv_data.DataBind()
    End Sub

#End Region
#Region "Event"
    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim Text As String = ""
        Select Case ddlMonthSearch.Text
            Case 1
                Text = "January"
            Case 2
                Text = "February"
            Case 3
                Text = "March"
            Case 4
                Text = "April"
            Case 5
                Text = "May"
            Case 6
                Text = "June"
            Case 7
                Text = "July"
            Case 8
                Text = "August"
            Case 9
                Text = "September"
            Case 10
                Text = "October"
            Case 11
                Text = "November"
            Case 12
                Text = "December"
        End Select

        lblShowMonth.Text = Text & " " & ddlYearSearch.Text
        bindData()
    End Sub
    Private Sub gv_data_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_data.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim UnitType_Service As New UnitType_service
                Dim Country_Service As New Country_Service
                Dim Brand_Service As New Brand_Service
                Dim Resort_Service As New Resort_service

                For index = 1 To 31
                    e.Row.Cells(index + 3).BackColor = Drawing.Color.White
                Next

                Dim iNight As Integer
                Dim iday As Integer
                iNight = CType(CType(e.Row.Cells(0).FindControl("lblNight"), Label).Text, Integer)
                iday = CType(CType(e.Row.Cells(1).FindControl("lblDayStart"), Label).Text, Integer)
                For index = 1 To iNight
                    e.Row.Cells(iday + index + 2).BackColor = Drawing.Color.DarkGray
                Next
                If ddlMonthSearch.SelectedValue = 2 Then
                    e.Row.Cells(34).Visible = False
                    e.Row.Cells(33).Visible = False
                    e.Row.Cells(32).Visible = False
                ElseIf (ddlMonthSearch.SelectedValue = 4) Or (ddlMonthSearch.SelectedValue = 6) _
                    Or (ddlMonthSearch.SelectedValue = 9) Or (ddlMonthSearch.SelectedValue = 12) Then
                    e.Row.Cells(34).Visible = False
                End If
                If Month(Now) = ddlMonthSearch.SelectedValue Then
                    For index = 1 To Day(Now)
                        e.Row.Cells(index + 3).BackColor = Drawing.Color.DarkGray
                    Next
                ElseIf Month(Now) > ddlMonthSearch.SelectedValue Then
                    For index = 1 To 34
                        e.Row.Cells(index + 3).BackColor = Drawing.Color.DarkGray
                    Next
                End If
            End If

            If e.Row.RowType = DataControlRowType.Header Then
                If ddlMonthSearch.SelectedValue = 2 Then
                    e.Row.Cells(34).Visible = False
                    e.Row.Cells(33).Visible = False
                    e.Row.Cells(32).Visible = False
                ElseIf (ddlMonthSearch.SelectedValue = 4) Or (ddlMonthSearch.SelectedValue = 6) _
                    Or (ddlMonthSearch.SelectedValue = 9) Or (ddlMonthSearch.SelectedValue = 12) Then
                    e.Row.Cells(34).Visible = False
                End If
            End If

        Catch ex As Exception

            ' Page.ClientScript.AlertMessage(Me, "gv_Data_RowDataBound", "gv_Data_RowDataBound >>" & ex.Message)

        End Try
    End Sub
    Protected Sub ddlBrandSearch_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlBrandSearch.SelectedIndexChanged
        If ddlBrandSearch.SelectedValue = "" Then
            BindResort()
        Else
            BindResort(ddlBrandSearch.SelectedValue)
        End If

    End Sub
#End Region
End Class